package com.xiran.srpingboottemplateself.service;

import com.xiran.srpingboottemplateself.mapper.BlogMapper;
import com.xiran.srpingboottemplateself.pojo.entity.Blog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BlogService {

    private final BlogMapper blogMapper;

    public List<Blog> getPublicBlogs() {
        return blogMapper.findPublicBlogs();
    }

    public Blog getBlogById(Long id, Long userId) {
        return blogMapper.findByIdWithAuth(id, userId);
    }

    public List<Blog> getUserBlogs(Long userId) {
        return blogMapper.selectByAuthorId(userId);
    }

    public boolean createBlog(Blog blog) {
        return blogMapper.insert(blog) > 0;
    }

    public boolean updateBlog(Blog blog) {
        return blogMapper.updateById(blog) > 0;
    }

    public boolean deleteBlog(Long id, Long userId) {
        Blog blog = blogMapper.selectById(id);
        if (blog != null && blog.getAuthorId().equals(userId)) {
            return blogMapper.deleteById(id) > 0;
        }
        return false;
    }

    public List<Blog> getAllBlogs() {
        return blogMapper.selectAll();
    }

    public boolean deleteBlogByAdmin(Long id) {
        return blogMapper.deleteById(id) > 0;
    }
}